package fastcsv.utils;

import java.io.IOException;
import java.io.RandomAccessFile;

public class DataGenerator {

	/**
	 * Repeat srcfile n times and save repeated data to dstfile.
	 * such as copy C:\\data\\an_example.csv ---100times---> C:\\data\\output.csv
	 * @param srcfile The source file to be repeated.
	 * @param dstfile A destination file to store the repeated data.
	 * @throws IOException
	 */
	public static void repaet_file(String srcfile, String dstfile, int n) throws IOException {
		byte[] bytes = read_bytes(srcfile);
		RandomAccessFile fp2 = new RandomAccessFile(dstfile, "rw");
		fp2.seek(fp2.length());
		for (int i = 0; i < n; i++)
			fp2.write(bytes);
		fp2.close();
	}

	/**
	 * Read bytes from files smaller than 2GB.
	 * 
	 * @param filepath The input file to read.
	 * @return a byte array
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	public static byte[] read_bytes(String filepath) throws IOException {
		RandomAccessFile fp1 = new RandomAccessFile(filepath, "r");
		if (fp1.length() > 2147483648L)
			throw new IOException();
		byte[] bytes = new byte[(int) fp1.length()];
		fp1.read(bytes);
		fp1.close();
		return bytes;
	}
}
